<?php

class JsondataController extends Zend_Controller_Action
{
    protected $db;
    
    public function init()
    {
        /* Initialize action controller here */
        /*if (!Zend_Auth::getInstance()->hasIdentity()) {
            $param = array(
            'action' => 'index',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );
            $this->_redirect($this->view->url($param, 'default', true));
        }
         *
         */

        $session = new Zend_Session_Namespace('Zend_Auth');
        $this->sess = $session->data;

        $sessionAdmin = new Zend_Session_Namespace('Zend_Auth_Admin');
        $this->sessAdmin = $sessionAdmin->data;

        if(!isset($this->sess) && !isset($this->sessAdmin)){
            $param = array(
            'action' => 'index',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );
            $this->_redirect($this->view->url($param, 'default', true));
        }

        

        $bootstrap = $this->getInvokeArg('bootstrap');
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->AjaxContext()
                ->addActionContext('get','json')
                ->addActionContext('post','json')
                ->addActionContext('new','json')
                ->addActionContext('edit','json')
                ->addActionContext('put','json')
                ->addActionContext('delete','json')
                ->initContext('json');

        $this->member = new Application_Model_Member();

        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

        $session->action = $this->param['action'];
    }

    

    public function indexAction()
    {
        //
    }

    public function saldoAction()
    {

        
        //$sess = new Zend_Session_Namespace();
        

        $accNum = $this->sess->member_code;
        $modelRek = new Application_Model_Rekening();
        $data = $modelRek->getAccountList($accNum);


        //$data = $this->member->getVM($memberId, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[1];

        

        echo json_encode($tmp);

    }


    public function daftartransferAction()
    {


        //$sess = new Zend_Session_Namespace();
        $memberId   = $this->sess->member_id;

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $modelRek = new Application_Model_Transfer();
        $data = $modelRek->getDaftarTransfer($memberId, $limit, $start);


        //$data = $this->member->getVM($memberId, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[1];



        echo json_encode($tmp);

    }



    public function mutasiAction()
    {

         //$sess = new Zend_Session_Namespace();
        $memberId   = $this->sess->member_id;
        $accNum = $this->sess->member_code;

        $transType = (isset($this->param['transType'])) ? $this->param['transType'] : "";

        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');
        
        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;
        
        $modelRek = New Application_Model_Rekening();
        $data = $modelRek->getMutasi($accNum, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[2];

        $debit = 0;
        $kredit = 0;
        foreach($data[2] as $dt){

            if ($dt["debkred"] == 'K' ){
                $kredit = $kredit + $dt["amt1"];
            }else{
                $debit = $debit + $dt["amt1"];
            }
        }


        $debit = number_format($debit, 2);
        $kredit = number_format($kredit, 2);


        //$tmp['footer'][] = array('amt1' => $kredit, 'acc_num1' => 'Average');
        $tmp['footer'][] = array('debit' => $debit, 'kredit' => $kredit, 'descr' => 'Sub Total');
        //$tmp['footer'][] = array('descr' => $kredit, 'trans_dtm' => 'Total Kredit');
        //$tmp['footer'][] = array('descr' => $debit, 'trans_dtm' => 'Total Debit');
       
        echo json_encode($tmp);
        
    }

    public function gensnAction()
    {

        $num = (isset($this->param['num'])) ? $this->param['num'] : "";
        $tmp = array();

        if ($num != ""){
            $limit = $this->param["rows"];
            $start = ($this->param["page"]-1)*$limit;


            $admin = new Application_Model_Admin();
            $data = $admin->genSN($num);

            $tmp['total'] = $data[0];
            $tmp['rows'] = $data[1];

            //echo json_encode($tmp);
        }

        echo json_encode($tmp);

    }

    public function cetaksnAction()
    {

        //$num = (isset($this->param['num'])) ? $this->param['num'] : "";
        $tmp = array();

        //if ($num != ""){
            $rows = $this->param["rows"];
            $page = $this->param["page"];

            $limit = $rows;
            $start = ($page-1)*$limit;

            /*
            $total = $rows*$page;

            if ($total > $num){
                $limit = $num - $start;

            }
             *
             */

            $sn = new Application_Model_Sn();
            $data = $sn->getSN(1, "", "", $start, $limit);

            $tmp['total'] = $data[0];
            $tmp['rows'] = $data[1];

            //echo json_encode($tmp);
        //}

        echo json_encode($tmp);

    }


    public function getsnAction()
    {

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $sn = (isset($this->param['sn'])) ? $this->param['sn'] : "";
        $stts = (isset($this->param['stts'])) ? $this->param['stts'] : "";


        $msn = new Application_Model_Sn();
        $data = $msn->getSN("", $sn, $stts, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[1];

        echo json_encode($tmp);
    }


    public function mutasiadminAction()
    {


        $transType = (isset($this->param['transType'])) ? $this->param['transType'] : "";
        $noAkun = (isset($this->param['noAkun'])) ? $this->param['noAkun'] : "";
        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $modelReport= New Application_Model_Report();
        $data = $modelReport->getMutasi($transType, $date1, $date2, $start, $limit, $noAkun);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[2];

        $debit = 0;
        $kredit = 0;
        foreach($data[2] as $dt){

            if ($dt["debkred"] == 'K' ){
                $kredit = $kredit + $dt["amt1"];
            }else{
                $debit = $debit + $dt["amt1"];
            }
        }


        $debit = number_format($debit, 2);
        $kredit = number_format($kredit, 2);


        //$tmp['footer'][] = array('amt1' => $kredit, 'acc_num1' => 'Average');
        $tmp['footer'][] = array('debit' => $debit, 'kredit' => $kredit, 'descr' => 'Sub Total');
        //$tmp['footer'][] = array('descr' => $kredit, 'trans_dtm' => 'Total Kredit');
        //$tmp['footer'][] = array('descr' => $debit, 'trans_dtm' => 'Total Debit');

        echo json_encode($tmp);

    }


    public function saldoadminAction()
    {


        $accNum = (isset($this->param['accNum'])) ? $this->param['accNum'] : "";

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;
        
        $modelReport = new Application_Model_Report();
        $data = $modelReport->getAccountList($accNum, $start, $limit);


        //$data = $this->member->getVM($memberId, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[1];



        echo json_encode($tmp);

    }

    public function infonetworkAction()
    {

        $memberId   = $this->sess->member_id;

        $parentCode = (isset($this->param['parent_code'])) ? $this->param['parent_code'] : "";
        $level = (isset($this->param['level'])) ? $this->param['level'] : "";

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $modelMember = new Application_Model_Member();
        $data = $modelMember->getInfoNetwork($memberId, $parentCode, $level, $start, $limit);


        //$data = $this->member->getVM($memberId, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[1];



        echo json_encode($tmp);

    }

    public function adminorderwithdrawAction()
    {

        //$this->member = new Application_Model_Member();
        //$data = $this->member->getAnggota($key, $start, $limit);

        //$auth = Zend_Auth::getInstance();
        //$identity = $auth->getIdentity();

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $appModel = new Application_Model_Approval();
        $data = $appModel->getOrderWithdraw($start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[2];

        $saldo = 0;
        foreach($data[2] as $dt){

            if ($dt["trans_type_id"] < 3 ){
                $saldo = $saldo + $dt["amt1"];
            }else{
                $saldo = $saldo - $dt["amt1"];
            }
        }


        $saldo = number_format($saldo, 0);


        //$tmp['footer'][] = array('amt1' => '19.80', 'acc_num1' => 'Average');
        //$tmp['footer'][] = array('amt_rp' => $saldo, 'acc_num1' => 'Total');

        echo json_encode($tmp);

    }


    public function adminordertopuppointAction()
    {

        //$this->member = new Application_Model_Member();
        //$data = $this->member->getAnggota($key, $start, $limit);

        //$auth = Zend_Auth::getInstance();
        //$identity = $auth->getIdentity();

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $appModel = new Application_Model_Approval();
        $data = $appModel->getOrderTopupPoint($start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[2];

        $saldo = 0;
        foreach($data[2] as $dt){

            if ($dt["trans_type_id"] < 3 ){
                $saldo = $saldo + $dt["amt1"];
            }else{
                $saldo = $saldo - $dt["amt1"];
            }
        }


        $saldo = number_format($saldo, 0);


        //$tmp['footer'][] = array('amt1' => '19.80', 'acc_num1' => 'Average');
        //$tmp['footer'][] = array('amt_rp' => $saldo, 'acc_num1' => 'Total');

        echo json_encode($tmp);

    }

    public function vouchercodeAction()
    {

        $slOperator = (isset($this->param['slOperator'])) ? $this->param['slOperator'] : "";

        $modelPurchase = new Application_Model_Purchase();
        $data = $modelPurchase->getVoucherCode($slOperator);

        echo json_encode($data);

    }

    public function paylistAction()
    {


        //$sess = new Zend_Session_Namespace();
        $memberId   = $this->sess->member_id;

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $modelPurch = new Application_Model_Purchase();
        $data = $modelPurch->getDaftarPembayaran($memberId, $limit, $start);


        //$data = $this->member->getVM($memberId, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[1];



        echo json_encode($tmp);

    }


    public function daftarpribadiAction()
    {
        $memberId   = $this->sess->member_id;
        $payFor = (isset($this->param['pay_for'])) ? $this->param['pay_for'] : "";

        $modelAcc = new Application_Model_Rekening();
        $data = $modelAcc->getPersonalAccByTransCode($memberId, $payFor);

        echo json_encode($data);

    }


    public function bonustrxAction()
    {


        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $accNum = $this->sess->member_code;
        $modelRek = new Application_Model_Rekening();
        $data = $modelRek->getBonusTransaction($accNum, $date1, $date2, $start, $limit);


        //$data = $this->member->getVM($memberId, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[1];



        echo json_encode($tmp);

    }


    public function trackingAction()
    {

        $transType = (isset($this->param['transType'])) ? $this->param['transType'] : "";
        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $accNum = $this->sess->member_code;
        $modelPurchase = new Application_Model_Purchase();
        $data = $modelPurchase->getTrackingTransaction($accNum, $transType, $date1, $date2, $start, $limit);


        //$data = $this->member->getVM($memberId, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[2];

        
        echo json_encode($tmp);

    }

    public function trackingadminAction()
    {

        $transType = (isset($this->param['transType'])) ? $this->param['transType'] : "";
        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $accNum = $this->param["noAkun"];
        $modelReport = new Application_Model_Report();
        $data = $modelReport->getTrackingTransaction($accNum, $transType, $date1, $date2, $start, $limit);


        //$data = $this->member->getVM($memberId, $transType, $date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[2];

        

        echo json_encode($tmp);

    }

    public function memberdetailAction()
    {

        $sn = $this->param["sn"];

        $clause = " (member_code like '%$sn%' or member_name like '%$sn%') ";

        $member = $this->member->getMemberByClause($clause, 0, 1);

        $num = count($member);
        if (count($member) > 0){
            $dataMember = $this->member->getMemberDetail($member[0]["member_id"]);

            echo json_encode(array_merge(array("cnt" => $num), $dataMember));
        }else{
            $data = array("cnt" => $num);

            echo json_encode($data);
        }
    }

    public function perkembanganmemberAction()
    {

        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');

        $limit = $this->param["rows"];
        $start = ($this->param["page"]-1)*$limit;

        $admin = new Application_Model_Member();
        $data = $admin->getPerkembanganMember($date1, $date2, $start, $limit);

        $tmp['total'] = $data[0];
        $tmp['rows'] = $data[1];

        echo json_encode($tmp);

    }
    
}

